/*
 * KendallsTauView.java
 */

package kendallstau;


import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;

/**
 * The application's main frame.
 */
public class KendallsTauView extends FrameView {


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        upperPanel = new javax.swing.JPanel();
        leftScrollPane = new javax.swing.JScrollPane();
        leftTextPane = new javax.swing.JTextPane();
        rightScrollPane = new javax.swing.JScrollPane();
        rightTextPane = new javax.swing.JTextPane();
        lowerPanel = new javax.swing.JPanel();
        evalButton = new javax.swing.JButton();
        clearButton = new javax.swing.JButton();
        uglySpacer = new javax.swing.JPanel();
        resultLabel = new javax.swing.JLabel();

        mainPanel.setMinimumSize(new java.awt.Dimension(500, 650));
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setLayout(new javax.swing.BoxLayout(mainPanel, javax.swing.BoxLayout.PAGE_AXIS));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(kendallstau.KendallsTauApp.class).getContext().getResourceMap(KendallsTauView.class);
        upperPanel.setBackground(resourceMap.getColor("upperPanel.background")); // NOI18N
        upperPanel.setMinimumSize(new java.awt.Dimension(100, 150));
        upperPanel.setName("upperPanel"); // NOI18N
        upperPanel.setLayout(new javax.swing.BoxLayout(upperPanel, javax.swing.BoxLayout.LINE_AXIS));

        leftScrollPane.setName("leftScrollPane"); // NOI18N

        leftTextPane.setName("leftTextPane"); // NOI18N
        leftScrollPane.setViewportView(leftTextPane);

        upperPanel.add(leftScrollPane);

        rightScrollPane.setName("rightScrollPane"); // NOI18N

        rightTextPane.setName("rightTextPane"); // NOI18N
        rightScrollPane.setViewportView(rightTextPane);

        upperPanel.add(rightScrollPane);

        mainPanel.add(upperPanel);

        lowerPanel.setMaximumSize(new java.awt.Dimension(32767, 35));
        lowerPanel.setMinimumSize(new java.awt.Dimension(100, 35));
        lowerPanel.setName("lowerPanel"); // NOI18N
        lowerPanel.setPreferredSize(new java.awt.Dimension(400, 35));
        lowerPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(kendallstau.KendallsTauApp.class).getContext().getActionMap(KendallsTauView.class, this);
        evalButton.setAction(actionMap.get("evalButtonClicked")); // NOI18N
        evalButton.setText("Evaluate");
        evalButton.setMaximumSize(new java.awt.Dimension(77, 40));
        evalButton.setName("evalButton"); // NOI18N
        lowerPanel.add(evalButton);

        clearButton.setAction(actionMap.get("clearButtonClicked")); // NOI18N
        clearButton.setText("Clear");
        clearButton.setName("clearButton"); // NOI18N
        lowerPanel.add(clearButton);

        uglySpacer.setMaximumSize(new java.awt.Dimension(100, 1));
        uglySpacer.setMinimumSize(new java.awt.Dimension(100, 1));
        uglySpacer.setName("uglySpacer"); // NOI18N
        uglySpacer.setPreferredSize(new java.awt.Dimension(100, 1));

        javax.swing.GroupLayout uglySpacerLayout = new javax.swing.GroupLayout(uglySpacer);
        uglySpacer.setLayout(uglySpacerLayout);
        uglySpacerLayout.setHorizontalGroup(
            uglySpacerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        uglySpacerLayout.setVerticalGroup(
            uglySpacerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1, Short.MAX_VALUE)
        );

        lowerPanel.add(uglySpacer);

        resultLabel.setText(resourceMap.getString("resultLabel.text")); // NOI18N
        resultLabel.setAlignmentX(10.0F);
        resultLabel.setName("resultLabel"); // NOI18N
        lowerPanel.add(resultLabel);

        mainPanel.add(lowerPanel);

        setComponent(mainPanel);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton clearButton;
    private javax.swing.JButton evalButton;
    private javax.swing.JScrollPane leftScrollPane;
    private javax.swing.JTextPane leftTextPane;
    private javax.swing.JPanel lowerPanel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel resultLabel;
    private javax.swing.JScrollPane rightScrollPane;
    private javax.swing.JTextPane rightTextPane;
    private javax.swing.JPanel uglySpacer;
    private javax.swing.JPanel upperPanel;
    // End of variables declaration//GEN-END:variables

    
    private List<String> leftElements;
    private List<String> rightElements;

    
    public KendallsTauView(SingleFrameApplication app) {
        super(app);
        initComponents();
        writeText("Paste list of items into text-box");

        leftElements = new ArrayList<String>();
        rightElements = new ArrayList<String>();
    }

    

    @Action
    public void evalButtonClicked() {
        if (leftTextPane.getText().trim().isEmpty() || rightTextPane.getText().trim().isEmpty())
        {
            writeError("Empty input");
            return;
        }

        loadElements(leftTextPane, leftElements);
        loadElements(rightTextPane, rightElements);

        if (leftElements.size() != rightElements.size())
        {
            writeError("Unequal number of entries");
            return;
        }

        for (String leftElm : leftElements)
        {
            if(!rightElements.contains(leftElm))
            {
                writeError("Item '" + leftElm + "' not found in right list");
                return;
            }
        }

        evaluateTau();
    }
    
    
    private void evaluateTau()
    {
        int count = leftElements.size();
        int concordant = 0;
        int discordant = 0;

        for(int left=0; left<count; left++)
        {
            String pivotLeft = leftElements.get(left);
            for(int i=left+1; i<count; i++)
            {
                String currentLeft = leftElements.get(i);

                int rightPivotIndex = -1;
                int rightCurrentIndex = -1;
                for (int right = 0; right < count; right++)
                {
                    if (rightElements.get(right).equals(pivotLeft))
                        rightPivotIndex = right;
                    if (rightElements.get(right).equals(currentLeft))
                        rightCurrentIndex = right;
                }

                if (rightPivotIndex == rightCurrentIndex)
                {
                    writeError("Error :'(");
                    return;
                }
                if (rightPivotIndex < rightCurrentIndex)
                    concordant++;
                else discordant++;
            }
        }

        double result = (double)(concordant - discordant) / ((double)count * 0.5 * (count - 1));
        writeText("Tau = " + result);
    }
    
    
    
    private void loadElements(javax.swing.JTextPane textBox, List<String> elements)
    {
        elements.clear();

        String[] lines = textBox.getText().split(".*\n");
        for (String line : lines)
        {
            if(!line.trim().isEmpty())
                elements.add(line.trim());
        }
    }
    

    @Action
    public void clearButtonClicked() {
        leftTextPane.setText("");
        rightTextPane.setText("");
        writeText("Paste list of items into both text-boxes");
    }
    
    
    
    private void writeText(String text)
    {
        resultLabel.setForeground(Color.BLACK);
        resultLabel.setText(text);
    }


    private void writeError(String text)
    {
        resultLabel.setForeground(Color.RED);
        resultLabel.setText(text);
    }
}
